/**
 * 
 */
var rowNumber;
/* 百度富文本编辑框初始化 */
var itemContentUrl;
$(function(){
	itemContentUrl = UM.getEditor('itemContentUrl');
	   $('#aditem_table')
		.datagrid(
				{
					url : 'admin/queryAdItemByCondition.do?pageSize=10',
					collapsible : true,
					width : '90%',
					height :'450px',
					iconCls : 'icon-man',
					fitColumns : true,
					pagination : true,
					rownumbers : true,
					singleSelect:true,
					checkOnSelect:false,
					selectOnCheck:false,
					loadMsg:'正在加载数据',
					loadFilter:function(data){
						 for(var i=0;i<data.rows.length;i++){
							 if(data.rows[i].itemStatus=='0'){
								 data.rows[i].itemStatus='可支持'
							 }
							 if(data.rows[i].itemStatus=='1'){
								 data.rows[i].itemStatus='不可支持'
							 }
							 if(data.rows[i].itemUseAuthCode){
								 data.rows[i].itemUseAuthCode='是';
							 }else{
								 data.rows[i].itemUseAuthCode='否';
							 }
							 if(data.rows[i].itemUseReceiveraddress){
								 data.rows[i].itemUseReceiveraddress='是';
							 }else{
								 data.rows[i].itemUseReceiveraddress='否';
							 }
							 if(data.rows[i].itemAwardStatus=='1'){
									data.rows[i].itemAwardStatus='不参与';
								}
							 if(data.rows[i].itemAwardStatus=='2'){
									data.rows[i].itemAwardStatus='参与抽奖';
								}
							 if(data.rows[i].itemAwardStatus=='3'){
									data.rows[i].itemAwardStatus='已抽奖';
								}
							 
						}  
						 return data;
					},
					onClickRow: function(rowIndex, rowData) {
						rowNumber = rowIndex;
						
					},
					onLoadSuccess:function(data){
						$('#aditem_table').datagrid('selectRow',rowNumber);
					},
					onDblClickRow :function(rowIndex,rowData){
						//layer.msg("double click");
					  },
					toolbar:'#toolbar',
					columns : [[
					{field:'projectTitle',title:'所属项目',width:250},
					{field:'itemTitle',title:'回报名称',width:150},
					{field:'itemSupportMoney',title:'需筹金额',width:100},
					{field:'itemSupportMaxCount',title:'最大人数',width:100},
					{field:'itemSupportCount',title:'已支持人数',width:150},
					{field:'itemContentUrl',title:'回报内容',width:100},
					{field:'itemStatus',title:'筹集状态',width:100},
					{field:'itemSequence',title:'回报排序',width:150},
					{field:'itemAwardStatus',title:'是否参与抽奖',width:150},
					{field:'itemUseAuthCode',title:'校验码是否使用',width:150},
					{field:'itemUseReceiveraddress',title:'是否需要收货地址',width:150}
					]]
				});
	   
	   //设置分页控件  
		var p = $('#aditem_table').datagrid('getPager');
		$(p).pagination({
			pageSize : 10,//每页显示的记录条数，默认为10  
			pageList : [5,10,20,30,40,50],//可以设置每页记录条数的列表  
			beforePageText : '第',//页数文本框前显示的汉字  
			afterPageText : '页    共 {pages} 页',
			displayMsg : '当前显示 {from} - {to} 条记录   共 {total} 条记录',
			onSelectPage:function(pageNumber, pageSize){ 
				//layer.msg("pageNumber="+pageNumber+"pageSize="+pageSize)
				var datas={
						projectTitle:$('#sprojectTitle').textbox('getValue'),
						itemTitle:$("#sitemTitle").textbox('getValue'),
						currentPage : (pageNumber-1)*pageSize,
						pageSize : pageSize
				}
				loadItemData(datas);
		    },
	 		onRefresh:function(pageNumber, pageSize){
	 			var datas={
	 					projectTitle:$('#sprojectTitle').textbox('getValue'),
						itemTitle:$("#sitemTitle").textbox('getValue'),
						currentPage : (pageNumber-1)*pageSize,
						pageSize : pageSize
				}
				loadItemData(datas);
	 		}
		}); 
   });

$(function(){
	$('#update').bind('click',function(){
		 var row = $('#aditem_table').datagrid('getSelected');
			if(row){
				$("#aditemdlg").form('clear');
				$("#itemContentUrl").html('');
				 $('#aditemdlg').dialog('open').dialog('setTitle','修改');
	                $('#aditemfm').form('load',row);
	                $("#aditemsaveUrl").val('admin/updateAditem.do?itemId='+row.itemId);
	                loadItemConten(row.itemContentUrl);
			}else{
				layer.msg('请选中一行!')
			}
	});

	$('#delete').bind('click',function(){
		var row = $('#aditem_table').datagrid('getSelected');
		if(row){
			if(row.itemSupportCount==0){
			$("#aditemsaveUrl").val('admin/deleteAditem.do?itemId='+row.itemId+'&itemTitle='+row.itemTitle);
			deleteItem();
			}else{
				layer.msg('已有人支持，禁止删除');
			}
		}else{
			layer.msg('请选中一行后再删除!');
		}
	});
});
	var loadItemData = function(datas){
			 $.ajax({
					url : 'admin/queryAdItemByCondition.do',
					type : "POST",
					dataType : "json",						
					data : datas,
					success : function(param) {
							$('#aditem_table').datagrid('loadData',param);
					}
			});   
		}
	//保存子项目
	function aditemSave(){
		var pageSize = $('#aditem_table').datagrid('getPager').data("pagination").options.pageSize;
		var pageNumber = $('#aditem_table').datagrid('getPager').data("pagination").options.pageNumber;
		var datas={
				currentPage : (pageNumber - 1) * pageSize,
				pageSize:pageSize
				};
		$.ajax({
			url : $("#aditemsaveUrl").val(),
			type : "POST",
			dataType : "json",
			data : {
				itemSequence:$("#itemSequence").numberbox('getValue'),
				itemSupportMoney:$("#itemSupportMoney").numberbox('getValue'),
				itemSupportMaxCount:$("#itemSupportMaxCount").numberbox('getValue'),
				itemContent:itemContentUrl.getContent(),
				itemTitle:$("#itemTitle").textbox('getValue'),
				itemaddress:$("#itemUseReceiveraddress").combobox('getValue'),
				AwardStatus:$('#AwardStatus').combobox('getValue')
			},
			beforeSend: function(){
				var flag=$("#aditemfm").form('enableValidation').form('validate');
		    		if(flag){
						$('#save').linkbutton({disabled:true}); 
		    			return flag;
		    		}else{
		    			return flag;
		    		}
	         },
			success : function(result) {
				$('#save').linkbutton({disabled:false}); 
				if (result.result == 0) {
					layer.msg(result.tips);
					//alert(result.tips);
				} else {
					layer.msg(result.tips);
					$('#aditemdlg').dialog('close'); // close the dialog
					$('#aditem_table').datagrid('reload',datas); // reload the  data
				}

			}
		});
	}
	//子项目的取消按钮
	function aditemCancel(){
		$('#save').linkbutton({disabled:false}); 
		$('#aditemdlg').dialog('close');
		$('#aditemfm').form('clear');
	}
	// delete btn
	function deleteItem(){
		var pageSize = $('#aditem_table').datagrid('getPager').data("pagination").options.pageSize;
		var pageNumber = $('#aditem_table').datagrid('getPager').data("pagination").options.pageNumber;
		var datas={
				projectTitle:$('#sprojectTitle').textbox('getValue'),
				itemTitle:$("#sitemTitle").textbox('getValue'),
				currentPage : (pageNumber-1)*pageSize,
				pageSize : pageSize
		}
		
		layer.confirm('是否确定删除？', {
			  btn: ['确定','点错了'] //按钮
			}, function(){
				$.ajax({
					url : $("#aditemsaveUrl").val(),
					type : "POST",
					dataType : "json",
					data:{},
					success : function(result) {
						if (result.result == 0) {
							layer.msg(result.tips);
						} else {
							layer.msg(result.tips);
							loadItemData(datas);
						}
					}
			});
			}, function(){
			  return;
			});
	}
	//search btn
	function searchItemBtn(){
		var pageSize=$('#aditem_table').datagrid('getPager').data("pagination").options.pageSize;
		var datas={
				pageSize:pageSize,
				itemTitle:$("#sitemTitle").textbox('getValue'),
				projectTitle:$('#sprojectTitle').textbox('getValue'),
		}
		loadItemData(datas);
	}
	//reset btn
	function resetItemBtn(){
		$('#sprojectTitle').textbox('setValue','');
		$("#sitemTitle").textbox('setValue','');
		var pageSize=$('#aditem_table').datagrid('getPager').data("pagination").options.pageSize;
		var datas={
				pageSize : pageSize,
		}
		loadItemData(datas);
	}
	//load itemcontent url and load data
	function loadItemConten(url){
		$("#itemContentUrl").load(url);
		layer.msg('项目详情加载完毕..');
	}